/**
 * 事件处理方法
 */
import {dealMksApi} from '@/views/youshuda/api/BaseApi'
import request from "@/utils/request";
import {strIsNotEmpty,escapeDeal} from "@/views/youshuda/utils/youshuda";

export const EventMixins = {
  components: {
  },
  props: {
  },
  computed: {},
  data() {
    return {
      // 表单参数
      form: {},
      //表单组件是否有效标识
      FormComponentActive: true,
      //表单设计字段配置信息
      formBuildConfig: [],
      //对象中下拉框
      formDataOptions: {},
    }
  },
  created() {
  },
  methods: {
    /**
     * ==============================表单组件事件处理==============================
     */
    /** 重新加载表单信息 **/
    refreshForm() {
      this.FormComponentActive = false;
      this.$nextTick(() => {
        this.FormComponentActive = true;
      });
    },
    /** 执行事件对应逻辑 **/
    evalEventCode(eventCode) {
      if (eventCode) {
        /** 特殊字符处理 **/
        eventCode = escapeDeal(eventCode);
        // 创建函数对象
        const func = new Function(eventCode);
        func.call(this);
      }
    },
    /** 获取表单字段值 **/
    getFieldValue(fieldName) {
      if (fieldName) {
        return this.form[fieldName]
      }
      return '';
    },
    /** 设置表单字段值 **/
    setFieldValue(fieldName, fieldValue) {
      if (fieldName) {
        this.$set(this.form, fieldName, fieldValue)
        this.changeFieldConfigValue(fieldName, 'fieldValue', fieldValue)
      }
    },
    /** 显示表单字段 **/
    showField(fieldName) {
      this.changeFieldConfigValue(fieldName, 'isShow',true)
    },
    /** 隐藏表单字段 **/
    hideField(fieldName) {
      this.changeFieldConfigValue(fieldName, 'isShow',false)
    },
    /** 改变element对象值 **/
    changeFieldConfigValue(fieldName, key, val) {
      if (!this.strIsNotEmpty(fieldName)) {
        this.msgError("处理字段不能为空");
        return;
      }
      for (let field of this.formBuildConfig) {
        if (field && (field['vModel'] == fieldName || field['formId'] == fieldName)) {
          this.$set(field, key, val)
          //如果是修改字段值，则需要同时修改defaultValue
          if(key == 'fieldValue'){
            this.$set(field, 'defaultValue', val)
          }
          return;
        }
      }
    },
    /** Get请求服务器数据 **/
    axiosGet(url) {
      if (!this.strIsNotEmpty(url)) {
        this.msgError("Get请求地址不能为空");
        return;
      }
      return request({url: url, method: 'get'})
    },
    /** Post请求服务器数据 **/
    axiosPost(url, data) {
      if (!this.strIsNotEmpty(url)) {
        this.msgError("Post请求地址不能为空");
        return;
      }
      return request({url: url, method: 'post', data: data})
    },
    /** 请求优树搭接口数据 **/
    getYsdApiData(procTarget,reqData){
      if (!this.strIsNotEmpty(procTarget)) {
        this.msgError("优树搭API编码不能为空");
        return;
      }
      return dealMksApi(procTarget,reqData);
    },


    /**
     * ==============================表单数据处理==============================
     */
    /** 同步数据 **/
    ItemValChange(data) {
      this.form[data.key] = data.val
    },
    /**
     * 同步表单值
     **/
    changeFormData(item) {
      if (item == null || item == undefined) {
        return
      }
      let fieldName = item.key;
      let fieldValue = item.val;
      this.setFieldValue(fieldName, fieldValue)
      /**
       * 下拉框联动的情况
       */
      if (item['type'] === 'select') {
        //值转成成以逗号隔开的字符串
        if (fieldValue instanceof Array) {
          let _fieldValue = fieldValue.filter(Boolean).join(',')
          this.$set(this.form, fieldName, _fieldValue)
        }
        this.getLinkVModelOption(item)
      }
    },
    /**
     * 下拉框联动的情况，获取联动下拉框选项数据
     * 返回结构：[{label: "是", value: "0"}, {label: "否", value: "1"}]
     */
    getLinkVModelOption(obj) {
      let that = this
      //联动字段选项集合key
      let itemKey = obj['config']['linkVModel']
      //联动字段选项集合key
      let linkVModelOptionUrl = obj['config']['linkVModelOptionUrl']
      //联动请求方式
      let linkVModelOptionReqType = obj['config']['linkVModelOptionReqType']
      if (!this.strIsNotEmpty(linkVModelOptionUrl) || !this.strIsNotEmpty(linkVModelOptionReqType)) {
        return
      }
      //当下拉框未选择是，清空被联动的下拉框
      if (!this.strIsNotEmpty(obj['val'])) {
        that.dealLinkVModelOption(itemKey, [])
      } else {
        if (linkVModelOptionReqType.toUpperCase() == 'POST') {
          //POST方式
          //发送请求
          this.axiosPost(linkVModelOptionUrl, {'dictType': obj['val']}).then(res => {
            that.dealLinkVModelOption(itemKey, res.data)
          })
        } else {
          //GET方式
          //发送请求
          this.axiosGet(linkVModelOptionUrl + '/' + obj['val']).then(res => {
            that.dealLinkVModelOption(itemKey, res.rows)
          })
        }
      }
    },
    /**
     * 处理被联动的下拉组件选择、值信息
     */
    dealLinkVModelOption(itemKey, optionData) {
      let that = this
      //重新赋值下拉选项
      that.$set(that.formDataOptions, itemKey, optionData)
      //重新赋值已选择下拉值
      let selectItem = ''
      for (let item of optionData) {
        if (that.formData[itemKey].indexOf(item['value']) > -1) {
          selectItem += item['value'] + ','
        }
      }
      if (this.strIsNotEmpty(selectItem)) {
        selectItem = selectItem.substr(0, selectItem.lastIndexOf(','))
      }
      that.$set(that.formData, itemKey, selectItem)
    },

    /**
     * 记录表单中类型为下拉框、多选框、单选框的选项值
     * @param item
     */
    changeFormDataOptions(item) {
      if (item == null || item == undefined) {
        return
      }
      this.$set(this.formDataOptions, item.key, item.val)
    },

  },
  watch: {

  }
}
